package com.lc.stock.module.crawldata.impl;

import com.alibaba.fastjson.JSONArray;
import com.lc.stock.common.CrawDataAPI;
import com.lc.stock.module.crawldata.AbstractCrawData;
import com.lc.stock.module.stockholdernum.dao.StockholderNumDao;
import com.lc.stock.module.stockholdernum.domain.StockholderNum;
import com.lc.stock.module.stockholdernum.domain.StockholderNumDetails;
import com.lc.stock.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lichen
 * @description 抓取公司股东户数 数据
 * @menu
 * @date 2023/2/7 9:17
 */
@Slf4j
@Service
public class CrawStockholderNumService extends AbstractCrawData {

    @Autowired
    private HttpClientUtil httpClientUtil;
    @Autowired
    private StockholderNumDao stockholderNumDao;

    /**
     * 抓取公司股东户数 数据
     * @param stockCode
     */
    @Override
    public void doCraw(String stockCode) {
        try{
            String resp = httpClientUtil.get(CrawDataAPI.STOCKHOLDER_NUM.getUrl(), stockCode);
            List<StockholderNumDetails> stockholderNumDetails = JSONArray.parseArray(resp, StockholderNumDetails.class);
            StockholderNum stockholderNum = new StockholderNum();
            stockholderNum.setStockCode(stockCode);
            stockholderNum.setStockholderNumList(stockholderNumDetails);

            //保存、更新数据库
            StockholderNum byStockCode = stockholderNumDao.findByStockCode(stockCode);
            if(null != byStockCode){
                stockholderNum.set_id(byStockCode.get_id());
            }
            stockholderNumDao.save(stockholderNum);
        }catch (Exception e){
            log.error("抓取公司" + stockCode + "股东户数数据",e);
        }
    }

    @Override
    public String getSubClassName() {
        return this.getClass().getName();
    }
}
